# coding: utf-8

# if 条件语句
if 0
	puts "0 is satisfied,something other than false or nil, this expression will be execute."
end

if 2 > 1 then
	puts "2 great than 1"
else
	puts "2 less than or equal to 1"
end

if 2 > 1 then
	puts "2 great than 1"
elsif 2 == 1 then
	puts "2 euqal to 1"
else
	puts "2 less than 1"
end

x = 2
name = 
	if x == 1 then "one"
	elsif x == 2 then "two"
	elsif x == 3 then "three"
	elsif x == 4 then "four"
	else "many"
	end
puts name

# if 作为关键字
puts "hi,if as a keyword!" if 1

# unless 条件语句
unless 2 <= 1 then
	puts "2 great than 1"
else
	puts "2 less than or equal to 1"
end

# case 语句
x = 2
name = 
    case
	when x == 1 then "one"
	when x == 2 then "two"
	when x == 3 then "three"
	when x == 4 then "four"
	else "many"
	end
puts name

name =
	case x
	when 1 then "one"
	when 2 then "two"
	when 3 then "three"
	when 4 then "four"
	else "many"
	end
puts name
			
name = 
    case
	when 1 === x then "one"
	when 2 === x then "two"
	when 3 === x then "three"
	when 4 === x then "four"
	else "many"
	end
puts name

puts \
	case x
	when String then "string"
    when Numeric then "number"
    when TrueClass, FalseClass then "boolean"
    else "other"
    end

# while and until

x = 1
while x <= 10 do
	puts x
	x += 1
end

x = 1
until x > 10 do
	puts x
	x += 1
end

x = 0                          # Initialize loop variable
puts x = x + 1 while x < 10    # Output and increment in a single expression

a = [1,2,3]                 # Initialize an array
puts a.pop until a.empty?   # Pop elements from array until empty

x = 10              # Initialize loop variable
begin               # Start a compound expression: executed at least once
  	puts x            #   output x
	x = x - 1         #   decrement x
end until x == 0    # End compound expression and modify it with a loop

x = 0               # Initialize loop variable
(                   # Start a compound expression: may be executed 0 times
	puts x            #   output x
	x = x - 1         #   decrement x
) until x == 0      # End compound expression and modify it with a loop

# for
# Print the elements in an array
array = [1,2,3,4,5]
for element in array 
	puts element
end

# Print the keys and values in a hash
hash = {:a=>1, :b=>2, :c=>3}
hash.each do |key,value|
  	puts "#{key} => #{value}"
end

hash = {:a=>1, :b=>2, :c=>3}
for key,value in hash
  	puts "#{key} => #{value}"
end

# 迭代器
4.upto(6) do |x| 
	puts x
end

puts "hello".enum_for(:each_char).map {|x| x.succ}

puts "hello".each_char.class

# 作用域，block会开启一个新的局部变量作用域，新的作用域不会覆盖外部作用域的变量，除非这些变量是参数。
total = 0   
[1, 2, 3].each {|x| total += x }
puts total

x = y = 0            # local variables
1.upto(3) do |x, y|   # x and y are local to block
                     # x and y "shadow" the outer variables
	y = x + 1          # Use y as a scratch variable
  	puts y * y           # Prints 4, 9, 16, 25
end
puts [x, y]                # => [0,0]: block does not alter these

# 异常处理

def add_one(x)
	raise ArgumentError, "x(#{x}) must great than 0!" if x <= 0
	puts x + 1
end

begin
	add_one 0
rescue ArgumentError => e
	puts e.message
end

begin
	add_one '0'
rescue ArgumentError, TypeError => e
	puts e.message
end

def explode # This method raises a RuntimeError 10% of the time
	raise "bam!" if rand(10) == 0
end

def risky
	begin # This block
		10.times do # contains another block
			explode # that might raise an exception.
		end # No rescue clause here, so propagate out.
	rescue TypeError # This rescue clause cannot handle a RuntimeError..
		puts $! # so skip it and propagate out.
	end
	
	"hello" # This is the normal return value, if no exception occurs.
end # No rescue clause here, so propagate up to caller.

def defuse
	begin # The following code may fail with an exception.
		puts risky # Try to invoke print the return value.
	rescue RuntimeError => e # If we get an exception
		puts e.message # print the error message instead.
		puts e.backtrace
	end
end

defuse